package com.tuanzhang.zhw.rpc.client;

import com.tuanzhang.zhw.rpc.codec.RpcDecoder;
import com.tuanzhang.zhw.rpc.codec.RpcEncoder;
import com.tuanzhang.zhw.rpc.codec.RpcRequest;
import com.tuanzhang.zhw.rpc.codec.RpcResponse;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;

public class RpcClientInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel channel) throws Exception {
        ChannelPipeline pipeline = channel.pipeline();
        // 编码器
        pipeline.addLast(new RpcEncoder(RpcRequest.class))
                //数据包解析规则:最大数据包大小，数据包起始位置,数据包头长度
                .addLast(new LengthFieldBasedFrameDecoder(65536,0,4,0,0))
                //解码器
                .addLast(new RpcDecoder(RpcResponse.class))
                //业务Handler
                .addLast(new RpcClientHandler());

    }
}
